<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of FacebookAlbums
 *
 * @author G011C1093
 */
require_once 'fb_setting.php';
require_once 'HTTP/Request2.php';
require_once ("facebook-php-sdk-master/src/facebook.php");

class FacebookAlbums {

	private $facebook;
	private $accessToken;
	private $userId;
	private $usrAlbum;
	private $usrList;
	private $usrPhotos;

	public function __construct($facebook, $accessToken, $userId) {
		$setting = new Setting();
		$this->facebook = $facebook;
		$this->accessToken = $accessToken;
		$this->userId = $userId;
	}

	private function conversionTime($time) {
		//timeの文字列を最適化する
		$time = substr($time, "0", "19");
		$time = strtr($time, "T", " ");
		return $time;
	}

	private function strToDate($str) {
		//timeをdate型に変換する
		$str = $this->conversionTime($str);
		return date('Y-n-j H:i:s', strtotime($str));
	}

	private function setAlbumImages($albumId) {
		//アルバムから写真を取り出して配列に格納する
		$photos = $this->facebook->api('/' . $albumId . '/photos', 'GET')['data'];
		$usrPhotos;

		$i = 0;
		foreach ($photos as $photo) {
			//写真の要素を配列に追加 
			$usrPhotos[$i]['album_id'] = $photo['id'];
			$usrPhotos[$i]['image'] = $photo['picture'];
			if (array_key_exists('name', $photo)) {
				$usrPhotos[$i]['name'] = $photo['name'];
			}
			$i++;
		}
		return $usrPhotos;
	}

	private function setFriendlist() {
		//友達のリストを作成
		$friends = $this->facebook->api('me/friends?fields=id,name,picture')['data'];
		$usrList;

		$i = 0;
		foreach ($friends as $friend) {
			$usrList[$i]['id'] = $friend['id'];
			$usrList[$i]['name'] = $friend['name'];
			$usrList[$i]['image'] = $friend['picture']['data']['url'];
			$i++;
		}
		$usrList = $this->setMyProfile($usrList);
		return $usrList;
	}

	private function setMyProfile($usrList) {
		//Userの情報を取得

		$profile = $this->facebook->api('me?fields=id,name,picture', 'GET');

		$mylist[0]['id'] = $profile['id'];
		$mylist[0]['name'] = $profile['name'];
		$mylist[0]['image'] = $profile['picture']['data']['url'];

		return array_merge($mylist, $usrList);
	}

	private function setAlbumTopImage($id) {
		//アルバムの最初の写真を返す
		$photos = $this->facebook->api('/' . $id . '/photos?fields=picture', 'GET')['data'];
		$url = "";
		foreach ($photos as $photo){
			if(array_key_exists('picture', $photo)){
							$url = $photo['picture'];
							break;
			}
		}
		return $url;
	}

	private function setFacebookAlbums($usrId) {
		//アルバム一覧を作成
		$albums = $this->facebook->api('/' . $usrId . '/albums', 'GET')['data'];
		$usrAlbum = null;

		$i = 0;
		foreach ($albums as $album) {
			$topimage = $this->setAlbumTopImage($album['id']);
			if ($topimage !== "") {
				$usrAlbum[$i]['id'] = $album['id'];
				$usrAlbum[$i]['image'] = $this->setAlbumTopImage($album['id']);
				if (array_key_exists('name', $album)) {
					$usrAlbum[$i]['name'] = $album['name'];
				}
				$i++;
			}
		}
		return $usrAlbum;
	}

	public function getFacebookUserAlbums($usrId) {
		//ユーザIDからアルバムを返す
		return $this->setFacebookAlbums($usrId);
	}

	public function getFacebookAlbumPhotos($albumId) {
		//アルバムIDから写真を返す
		return $this->setAlbumImages($albumId);
	}

	public function getFriendList() {
		//友達一覧を返す
		return $this->setFriendlist();
	}

}

?>
